package thread;

import java.io.IOException;
import java.nio.CharBuffer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 线程池
 * 线程池是线程的管理机制,它主要解决两个问题
 */
public class ThreadPoolDemo {
    public static void main(String[] args) {
        //1/创建线程池
        ExecutorService threadPool= Executors.newFixedThreadPool(3);

        for (int i = 0; i < 5; i++) {
            Runnable r=new Runnable() {
                public void run(){
                    try {
                        Thread t=Thread.currentThread();
                        System.out.println(t.getName()+"正在执行任务...");
                        Thread.sleep(5000);
                        System.out.println(t.getName()+"执行完毕");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            threadPool.execute(r);
            System.out.println("将一个任务交给了线程池");

        }
        threadPool.shutdown();//任务执行完关闭
        //threadPool.shutdownNow();//直接关闭
        System.out.println("线程池关闭了");

    }
}
